package com.example.jbmsuppliers.sinopec.activity;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.net.NetWork;
import com.example.jbmsuppliers.sinopec.utlis.ExitAppUtlis;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IEGuardKey;
import com.jbm.jbmsupplier.util.IEGuardKeyCtrl;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 李征
 * Created by 家帮忙 on 2016/11/21.
 */

public class MyAddressAll extends ActivityUse {

    @InjectView(R.id.goback_ps_set)
    Button gobackPsSet;
    @InjectView(R.id.titlelz)
    TextView titlelz;
    @InjectView(R.id.id_door_address_list)
    ListView idDoorAddressList;
    private ArrayList<IEGuardKey> mDataList = new ArrayList<IEGuardKey>();
    DooarAddressAdapter mDooarAdapter = null;
    public static final int LOOKADDRESSMESS = 0001;
    DialogLoading dialog;

    @Override
    protected int setContentView1() {
        return R.layout.activity_door_manage;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);
        ExitAppUtlis.getInstance().addActivity(this);

        if (getIntent().getStringExtra("dizhi").equals("dizhi")) {
            titlelz.setText("地址信息");
        }

        NetWork networt = new NetWork(handler);
        networt.getLookAddress();
        dialog = new DialogLoading(this, "加载中...", true);
        dialog.show();
    }


    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case LOOKADDRESSMESS: {
                    mDataList.clear();
                    IEGuardKeyCtrl restlu = (IEGuardKeyCtrl) msg.obj;
                    if (restlu.back) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        for (int i = 0; i < restlu.count; i++) {
                            IEGuardKey resultlist = restlu.IndexofList(i);
                            if (resultlist.open_date == null) {
                                mDataList.add(resultlist);
                            }
                        }
                        mDooarAdapter = new DooarAddressAdapter(MyAddressAll.this, mDataList);
                        idDoorAddressList.setAdapter(mDooarAdapter);
                        mDooarAdapter.notifyDataSetChanged();

                    } else if (restlu.equals("time out")) {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint(restlu.errorinfo);

                    } else {
                        if (dialog != null) {
                            dialog.dismiss();
                        }
                        hint(restlu.errorinfo);
                    }


                    break;
                }
            }

        }
    };

    @OnClick(R.id.goback_ps_set)
    public void onClick() {
        finish();
    }

    class DooarAddressAdapter extends BaseAdapter {
        private Context context = null;
        public ArrayList<IEGuardKey> mAddressList = null;

        public DooarAddressAdapter(Context context, ArrayList<IEGuardKey> inList) {
            this.context = context;
            this.mAddressList = inList;
        }

        @Override
        public int getCount() {
            return mAddressList.size();
        }

        @Override
        public Object getItem(int position) {
            return mAddressList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            try {
                if (convertView == null) {
                    convertView = LayoutInflater.from(this.context).inflate(R.layout.door_item, null);
                }
                TextView addressText = (TextView) convertView.findViewById(R.id.door_item_address);
                final IEGuardKey info = mAddressList.get(position);
                if (info == null)
                    return null;
                if (info.gi_fulltext != null)
                    addressText.setText(info.gi_fulltext);
                else
                    addressText.setText(info.open_key);
                convertView.setTag(info);

                return convertView;
            } catch (Exception e) {
                String msg = e.getMessage();
                return null;
            }
        }


    }
}
